package com.cdu.practicaltraining.sys.handler;

import com.cdu.practicaltraining.pojo.Role;
import com.cdu.practicaltraining.sys.service.RoleService;
import com.cdu.practicaltraining.utils.CommonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/role")
public class RoleHandler {
    @Resource
    RoleService roleService;

    /**
     * 添加角色
     * @param role
     * @return
     */
    @PostMapping("/add")
    public CommonResult<Role> add(@RequestBody Role role) {
        try {
            int i = roleService.add(role);
            if (i > 0) {
                return CommonResult.success(role);
            }
            if (i == 0) {
                return CommonResult.error(500, "添加角色失败");
            }
            return CommonResult.error(-1, "您要添加的角色信息已经存在,请换一个.");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(503, "系统异常,添加失败,请联系管理员.");
        }
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/roles/{pageNum}/{pageSize}")
    public CommonResult<PageInfo<Role>> roles(@PathVariable("pageNum") int pageNum,@PathVariable("pageSize") int pageSize){
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Role> roles = roleService.roles();
            PageInfo<Role> pageInfo = new PageInfo<>(roles);
            pageInfo.setPageNum(pageNum);
            pageInfo.setPageSize(pageSize);
            return roles.size() > 0? CommonResult.success(pageInfo):CommonResult.error(401,"查询失败");
        }catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(503,"系统异常，查询失败，请联系管理员");
        }
    }

    /**
     * 根据id查询最新角色数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult<Role> findNewRole(@PathVariable("id") long id) {
        try {
            Role role = roleService.findById(id);
            return role!=null? CommonResult.success(role):CommonResult.error(401,"查询失败");
        }catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(503,"系统异常，查询失败，请联系管理员");
        }
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @PutMapping("/update")
    public CommonResult<Role> updateRole(@RequestBody Role role) {
        try {
            int i = roleService.updateRole(role);
            return i!=0?CommonResult.success(role):CommonResult.error(500,"修改角色失败");
        }catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(503,"系统异常，修改失败，请联系管理员");
        }
    }

    /**
     * 逻辑删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public CommonResult deleteDict(@Param("id") long id) {
        try {
            int i = roleService.deleteRole(id);
            return i!=0?CommonResult.success(i):CommonResult.error(500,"删除角色失败");
        }catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(503,"系统异常，删除失败，请联系管理员");
        }
    }
}
